﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace Ewts.Code
{
    public class RDLCPrinter
    {
        private const int MaxRowFirstPageV = 25;
        private const int MaxRowV = 25;

        private const int MaxRowFirstPageH = 20;
        private const int MaxRowH = 20;

        /// <summary>
        /// 为报表添加空行，填充纸张
        /// </summary>
        /// <param name="dt">数据集</param>
        /// <param name="type">竖版页面为1，横版页面为2，特例为else，不用执行任何操作</param>
        /// <param name="rowNum1">第一页行数（如果type不为3，由本方法指定）</param>
        /// <param name="rowNumN">第二页开始的行数（如果type不为3，由本方法指定）</param>
        public void FillRow(DataTable dt, int type, int rowNumFirstPage, int rowNum)
        {
            int oldRowTotal = dt.Rows.Count;        //本身的行数
            for (int c = 0; c < dt.Columns.Count; c++)
            {
                dt.Columns[c].AllowDBNull = true;
            }

    //        int rowSign = 0;
            if (type == 1)
            {
                rowNumFirstPage = MaxRowFirstPageV;
                rowNum = MaxRowV;
               // rowSign = 3;
            }
            else if (type == 2)
            {
                rowNumFirstPage = MaxRowFirstPageH;
                rowNum = MaxRowH;
                //rowSign = 2;
            }
            //else
            //{
            //    rowSign = 1;
            //}

            if (oldRowTotal <= rowNumFirstPage)
            {
                int sum = rowNumFirstPage - dt.Rows.Count;
                for (int i = 0; i < sum; i++)
                {
                    dt.Rows.Add(dt.NewRow());
                }
            }
            else
            {
                //
                //int pageCount = (dt.Rows.Count - (rowNumFirstPage + rowSign)) / (rowNum + rowSign);//除第一页和最后一页以外的页数
                //int sum = (pageCount * (rowNum + rowSign) + rowNum) - (dt.Rows.Count - (rowNumFirstPage + rowSign));//需要补齐的行数
                 int pageCount = 0;
                 pageCount = oldRowTotal / rowNum;
                if (rowNum * pageCount != oldRowTotal)
                {
                    pageCount += 1;
                }
                int sum = pageCount * rowNum;
                
                for (int j = 0; j < sum-oldRowTotal; j++)
                {
                    dt.Rows.Add(dt.NewRow());
                }
            }
        }
    }
}